package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.PropertyUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.model.SysProperty;
import com.ccp.dev.system.service.SysPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统属性Controller
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.SYSTEM_SETTING)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/sysProperty")
public class SysPropertyController extends SysBaseController {

    @Autowired
    private SysPropertyService sysPropertyService;

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: sysProperty 保存的对象
     * @return: java.lang.Object
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="编辑系统属性信息",exectype = "编辑日志")
    public Object save(@RequestBody SysProperty sysProperty){
        sysPropertyService.save(sysProperty);
        return getResultDataSuccess("save.success");
    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"id"
     * @return: java.lang.Object
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除系统属性数据",exectype = "删除日志")
    public Object delete(@RequestParam("id") String id) {
        if (StringUtil.isEmpty(id)) {
            return fail("delete.failure.message");
        }
        int result = sysPropertyService.delByIdArray(id.split(","));
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }

    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"id"
     * @return: java.lang.Object
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @Action(description = "查询系统属性详情", exectype = "查询日志")
    @GetMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    public ResultData getSysPropertyById(@RequestParam("id") String id){
        SysProperty sysProperty = sysPropertyService.getOneById(id);
        if (null == sysProperty) {
            return getResultDataError("get.failure");
        }
        return getResultDataSuccess("get.success",sysProperty);
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @GetMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="系统属性附带检索条件的分页查询", exectype = "查询日志")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        List<SysProperty> pageDatas = sysPropertyService.queryList(queryFilter);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }

    /**
     * 获取消息弹框value值
     * @return
     */
    @RequestMapping("/getPropertyByAlias")
    @ResponseBody
    public ResultData getPropertyByAlias(){
        return getResultDataSuccess("operation.success", PropertyUtil.getBooleanByAlias("messageRemind"));
    }

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "系统属性列表页面跳转",exectype = "页面跳转日志")
    public String list() {
        return "sysProperty/list";
    }

    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description = "系统属性编辑页面跳转",exectype = "页面跳转日志")
    public String add(String id, ModelMap modelMap) {
        Integer sn = sysPropertyService.getMaxSn();
        sn = BeanUtils.isEmpty(sn)?1:++sn;
        modelMap.put("id",StringUtil.isEmpty(id)?"":id);
        modelMap.put("sn",sn);
        return "sysProperty/add";
    }

    /**
     * 功能描述: 跳转到详情页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2019-09-26 09:46:55
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_GET)
    @Action(description = "系统属性编辑页面跳转",exectype = "页面跳转日志")
    public String get(@RequestParam("id") String id, ModelMap modelMap) {
        modelMap.put("id",id);
        return "sysProperty/get";
    }

}
